package cn.wolfcode.car.business.web.controller;

import cn.wolfcode.car.base.domain.User;
import cn.wolfcode.car.base.service.IUserService;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import cn.wolfcode.car.shiro.ShiroUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;

/**
 * 服务明细单控制器
 */
@Controller
@RequestMapping("business/statementItem")
public class StatementItemController {
    //模板前缀
    private static final String prefix = "business/statementItem/";

    @Autowired
    private IStatementItemService statementItemService;
    @Autowired
    private IStatementService statementService;
    @Autowired
    private IUserService userService;

    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("business:statementItem:view")
    @RequestMapping("/listPage")
    public String listPage() {
        return prefix + "list";
    }

    @RequiresPermissions("business:statementItem:add")
    @RequestMapping("/addPage")
    public String addPage() {
        return prefix + "add";
    }

    @RequiresPermissions("business:statementItem:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model) {
        model.addAttribute("statementItem", statementItemService.get(id));
        return prefix + "edit";
    }

    //数据-----------------------------------------------------------
    //列表
    @RequiresPermissions("business:statementItem:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<StatementItem> query(StatementItemQuery qo, Long statementId) {
        return statementItemService.query(qo, statementId);
    }

    //新增
    @RequiresPermissions("business:statementItem:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(StatementItem statementItem) {
        statementItemService.save(statementItem);
        return AjaxResult.success();
    }

    //编辑
    @RequiresPermissions("business:statementItem:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(StatementItem statementItem) {
        statementItemService.update(statementItem);
        return AjaxResult.success();
    }

    //删除
    @RequiresPermissions("business:statementItem:remove")
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        statementItemService.deleteBatch(ids);
        return AjaxResult.success();
    }

    @RequiresPermissions("business:statementItem:itemDetail")
    @RequestMapping("/itemDetail")
    public String editDetail(Long statementId, Model model) {
        Statement statement = statementService.get(statementId);
        model.addAttribute("statement", statement);
        //如果状态为消费中，则跳转到明细编辑页
        if (statement.getStatus().equals(Statement.STATUS_CONSUME)) {
            return prefix + "editDetail";
        } else {
            //方法一：
//            Long userId = ShiroUtils.getUserId();
//            statement.setPayee(userService.get(userId));
            //方法二：
            User payee = userService.get(statement.getPayeeId());
            statement.setPayee(payee);
            return prefix + "showDetail";
        }
    }

    @RequiresPermissions("business:statementItem:saveItems")
    @RequestMapping("/saveItems")
    @ResponseBody
    public AjaxResult saveItems(@RequestBody List<StatementItem> items) {
        statementItemService.saveItems(items);
        return AjaxResult.success();
    }

    @RequiresPermissions("business:statementItem:payStatement")
    @RequestMapping("/payStatement")
    @ResponseBody
    public AjaxResult payStatement(Long statementId) {
        statementItemService.payStatement(statementId);
        return AjaxResult.success();
    }

    @RequestMapping("/export")
    @ResponseBody
    public void export(HttpServletResponse response, Long statementId) throws IOException, ParseException {
        response.setHeader("Content-Disposition", "attachment;filename=showData.xlsx");
        Workbook workbook = statementItemService.export(statementId);
        workbook.write(response.getOutputStream());
    }

}
